package cn.iocoder.yudao.module.famous.doctor.controller.admin.treatmentuser;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.researchgroup.vo.ResearchgroupClinicBaseVO;
import cn.iocoder.yudao.module.famous.doctor.dal.mysql.researchgroup.ResearchgroupMapper;
import jodd.util.StringUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.treatmentuser.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.treatmentuser.TreatmentUserDO;
import cn.iocoder.yudao.module.famous.doctor.convert.treatmentuser.TreatmentUserConvert;
import cn.iocoder.yudao.module.famous.doctor.service.treatmentuser.TreatmentUserService;

@Tag(name = "管理后台 - 就诊人")
@RestController
@RequestMapping("/famous.doctor/treatment-user")
@Validated
public class TreatmentUserController {

    @Resource
    private TreatmentUserService treatmentUserService;

    @Resource
    private ResearchgroupMapper researchgroupMapper;

    @PostMapping("/create")
    @Operation(summary = "创建就诊人")
    @PreAuthorize("@ss.hasPermission('famous.doctor:treatment-user:create')")
    public CommonResult<Long> createTreatmentUser(@Valid @RequestBody TreatmentUserCreateReqVO createReqVO) {
        return success(treatmentUserService.createTreatmentUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新就诊人")
    @PreAuthorize("@ss.hasPermission('famous.doctor:treatment-user:update')")
    public CommonResult<Boolean> updateTreatmentUser(@Valid @RequestBody TreatmentUserUpdateReqVO updateReqVO) {
        treatmentUserService.updateTreatmentUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除就诊人")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:treatment-user:delete')")
    public CommonResult<Boolean> deleteTreatmentUser(@RequestParam("id") Long id) {
        treatmentUserService.deleteTreatmentUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得就诊人")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:treatment-user:query')")
    public CommonResult<TreatmentUserRespVO> getTreatmentUser(@RequestParam("id") Long id) {
        TreatmentUserDO treatmentUser = treatmentUserService.getTreatmentUser(id);
        return success(TreatmentUserConvert.INSTANCE.convert(treatmentUser));
    }

    @GetMapping("/list")
    @Operation(summary = "获得就诊人列表")
    @PreAuthorize("@ss.hasPermission('famous.doctor:treatment-user:query')")
    public CommonResult<List<TreatmentUserRespVO>> getTreatmentUserList() {
        List<TreatmentUserDO> list = treatmentUserService.getTreatmentUserList();
        List<TreatmentUserRespVO> treatmentUserRespVOS = TreatmentUserConvert.INSTANCE.convertList(list);
        for (int i = 0; i < treatmentUserRespVOS.size(); i++) {
            if (StringUtil.isNotBlank(treatmentUserRespVOS.get(i).getIdentityCard())) {
                List<ResearchgroupClinicBaseVO> selectclinic = researchgroupMapper.selectclinic(3, treatmentUserRespVOS.get(i).getIdentityCard());
                if (selectclinic.size()>0) {
                    treatmentUserRespVOS.get(i).setClinicType(1L);
                }
            }
        }
        return success(treatmentUserRespVOS);
    }

    @GetMapping("/page")
    @Operation(summary = "获得就诊人分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:treatment-user:query')")
    public CommonResult<PageResult<TreatmentUserRespVO>> getTreatmentUserPage(@Valid TreatmentUserPageReqVO pageVO) {
        PageResult<TreatmentUserDO> pageResult = treatmentUserService.getTreatmentUserPage(pageVO);
        return success(TreatmentUserConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出就诊人 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:treatment-user:export')")
    @OperateLog(type = EXPORT)
    public void exportTreatmentUserExcel(@Valid TreatmentUserExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<TreatmentUserDO> list = treatmentUserService.getTreatmentUserList(exportReqVO);
        // 导出 Excel
        List<TreatmentUserExcelVO> datas = TreatmentUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "就诊人.xls", "数据", TreatmentUserExcelVO.class, datas);
    }

}
